//--------------Sonic CD Credits Control Script---------------//
//--------Scripted by Christian Whitehead 'The Taxman'--------//
//-------Unpacked By Rubberduckycooly's Script Unpacker-------//

// Aliases
#alias Object.Value0	:	Object.Timer
// Value1 is unused...
#alias Object.Value2	:	Object.Scroll
#alias Object.Value3	:	Object.EndCredits

// Text Font Aliases
#alias Object.Value1	:	Object.TargetLine

// States
#alias 0	:	CREDITSCONTROL_INIT
#alias 1	:	CREDITSCONTROL_FADEIN
#alias 2	:	CREDITSCONTROL_MAIN
#alias 3	:	CREDITSCONTROL_FADEOUT

// Soundtrack
#alias 0	:	OST_JP

// Presentation Stage
#alias 1	:	STAGE_P_MENU

// Priority
#alias 1	:	PRIORITY_ACTIVE

// Engine States
#alias 8	:	ENGINE_WAIT

// Text Info type
#alias 0	:	TEXTINFO_TEXTDATA
#alias 1	:	TEXTINFO_TEXTSIZE
#alias 2	:	TEXTINFO_ROWCOUNT


sub ObjectMain
	switch Object.State
	case CREDITSCONTROL_INIT
		
		// Object.Scroll should be 0 at this point, so this is
		// essentially resetting the Camera Y Offset to be 0
		Screen.YOffset = Object.Scroll
		
		// Center the camera to be around 256 pixels in, to make sure the text is centered
		TempValue0	   = 256
		TempValue0	  -= Screen.CenterX
		
		Screen.XOffset = TempValue0
		
		// This Object is what draws the backgound, so it needs to draw low
		Object.DrawOrder = 0
		
		if Object.Timer < 8
			// Hold for a moment...
			
			Object.Timer++
		else
			// Start fading in
			
			Object.Timer = 254
			PlayMusic(0)
			Object.State++
		end if
		
		// Set the screen to be black for now
		SetScreenFade(0, 0, 0, 255)
		break
		
	case CREDITSCONTROL_FADEIN
		if Object.Timer > 0
			SetScreenFade(0, 0, 0, Object.Timer)
			
			Object.Timer -= 8
		else
			Object.State++
		end if
		break
		
	case CREDITSCONTROL_MAIN
		Object.Scroll++
		
		// Speed up the credits if the player wants
		if KeyDown[0].ButtonA == true
			Object.Scroll++
		end if
		
		if KeyDown[0].ButtonC == true
			Object.Scroll++
		end if
		
		TempValue0   = Object.Scroll
		TempValue0 >>= 1
		
		Screen.YOffset = TempValue0
		
		// If at the end of the Credits, then fade out
		if Object.Scroll > Object.EndCredits
			Object.State = CREDITSCONTROL_FADEOUT
			Object.Timer = 0
		end if
		
		// And then, let the Player just skip the Credits if they want to
		
		if KeyPress[0].Start == true
			Object.State = CREDITSCONTROL_FADEOUT
			Object.Timer = 0
		end if
		
		if KeyPress[0].ButtonB == true
			Object.State = CREDITSCONTROL_FADEOUT
			Object.Timer = 0
		end if
		
		CheckTouchRect(0, 0, Screen.XSize, Screen.YSize)
		if CheckResult > -1
			Object.State = CREDITSCONTROL_FADEOUT
			Object.Timer = 0
		end if
		break
		
	case CREDITSCONTROL_FADEOUT
		if Object.Timer < 320
			Object.Timer += 8
			Music.Volume -= 2
		else
			StopMusic()
			
			// Go back to the menu now
#platform: Use_Origins	// Call Origins movie
			Engine.State = ENGINE_WAIT
			
			// Unlock ending on the Museum
			game.callbackParam0 = true
			EngineCallback(NOTIFY_STATS_MOVIE)
#endplatform

#platform: Use_Standalone
			Stage.ActiveList = PRESENTATION_STAGE
			Stage.ListPos = STAGE_P_MENU
			LoadStage()
#endplatform
		end if
		
		// Have a maximum fade value of 255, since that's all that a byte can hold
		TempValue0 = Object.Timer
		if TempValue0 > 255
			TempValue0 = 255
		end if
		
		SetScreenFade(0, 0, 0, TempValue0)
		break
	
	end switch
	
end sub


sub ObjectDraw
	
	// The scene doesn't have a background or anything, so draw a Black Rectangle here to avoid frame smear
	DrawRect(0, 0, Screen.XSize, Screen.YSize, 0, 0, 0, 255)
	
end sub


sub ObjectStartup
	// There's no exact "credits" song for either region, per se, so
	// these tracks are used instead
		SetMusicTrack("JP/Credits.ogg", 0, 1)
	
	// Note: On Standard Platforms (ie PC 2012 & consoles) "Data/Game/Credits_Console.txt" is used instead
	// (Though, Origins does indeed use the "Mobile" file, and mobile versions of couse do too)
	LoadTextFile(MENU_1, "Data/Game/Credits_Mobile.txt", false)
	
	// In this scene, we control the camera manually
	Screen.CameraEnabled = false
	
	// Place the Credits Control object into the scene
	Object[0].Type = TypeName[Credits Control]
	
	// Make sure that it's always active, since it's the master control in this scene
	Object[0].Priority = PRIORITY_ACTIVE
	
	// Get the row count of how many entries there are in the credits file
	GetTextInfo(TempValue1, MENU_1, TEXTINFO_ROWCOUNT, 0, 0)
	
	// Find the first unoccupied object slot in the stage
	ArrayPos0 = 32
	while Object[ArrayPos0].Type != TypeName[Blank Object]
		ArrayPos0++
	loop
	
	// Create all the Credits text
	// - TempValue0 is the current line being read from the file
	// - TempValue2 is the X Position to spawn the text at (stays constant)
	// - TempValue3 is the Y Position to spawn the text at
	
	// Starting values:
	
	// Starting line should be 0 (ofc)
	TempValue0 = 0
	
	// X Position should be 256 pixels to the right
	TempValue2 = 0x1000000
	
	// Starting Y Position should be 256 pixels as well
	TempValue3 = 0x1000000
	
	// Now, let's enter the loop!
	while TempValue0 < TempValue1
		
		// Get what type of entry it is
		// [Reading the number inside the brackets]
		GetTextInfo(TempValue4, MENU_1, TEXTINFO_TEXTDATA, TempValue0, 1)
		
		// Get the length of the entry
		GetTextInfo(TempValue5, MENU_1, TEXTINFO_TEXTSIZE, TempValue0, 0)
		
		if TempValue5 == 0
			// This is a blank line, so don't put any text on the current line and instead create a new line 22 pixels tall
			TempValue3 += 0x160000
		else
			if TempValue4 == 48 // '0' character
				ResetObjectEntity(ArrayPos0, TypeName[Text Font 1], 0, TempValue2, TempValue3)
				Object[ArrayPos0].TargetLine = TempValue0
				ArrayPos0++
				
				// 22 pixels difference
				TempValue3 += 0x160000
			else
				if TempValue4 == 49 // '1' character
					ResetObjectEntity(ArrayPos0, TypeName[Text Font 2], 0, TempValue2, TempValue3)
					Object[ArrayPos0].TargetLine = TempValue0
					ArrayPos0++
					
					// 14 pixels difference
					TempValue3 += 0xE0000
				else
					if TempValue4 == 50 // '2' character
						ResetObjectEntity(ArrayPos0, TypeName[Text Font 3], 0, TempValue2, TempValue3)
						Object[ArrayPos0].TargetLine = TempValue0
						ArrayPos0++
						
						// 14 pixels difference
						TempValue3 += 0xE0000
					end if
				end if
			end if
		end if
		
		TempValue0++
	loop
	
	TempValue3 -= 0xE0000 // 14 pixels
	Object[0].EndCredits   = TempValue3
	Object[0].EndCredits >>= 15
	
end sub


// ========================
// Editor Subs
// ========================

sub RSDKDraw
	DrawSprite(0)
end sub


sub RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143) // "Script" Icon

	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end sub
